# -*- coding: utf-8 -*-
import numpy
import math

class price_model(object):

    def __init__(self, price_list):
        # Resources in micro grid
        self.price_list = self.price_list

        self.n_level = None

        self.price_levels = None

        self.initialize(price_list)

    def initialize(self):

        self.price_levels = list(set(self.price_list))

        self.n_level = len(self.price_levels)

        self.price_label = 0*self.price_list

        for i in range(0, len(self.price_list)):
            for j in range(0, len(self.price_levels)):
                if self.price_list[i] == self.price_levels[j]:
                    self.price_label[i] = j
                    break

    # Based on the list of price
    def flow_direction(self):
        # Can be user-defined rules
        flow_direction = [0] * 24
        for i in range(24):
            if self.price_label[i] == 2:  # Do not buy from grid if possible
                flow_direction[i] = 1  #
            elif self.price_label[i] == 1:  # Do not operate on battery
                if self.load[i] > self.pv[i]:
                    flow_direction[i] = 1
                else:
                    flow_direction[i] = -1
            elif self.price_label[i] == 0:  # Buy from grid
                flow_direction[i] = -1

        return flow_direction

    def charge_direction(self):
        """User Defined Capacity Rule
         
        Accepts a pyomo Concrete Model as the first positional argument,
        and and a plant index as a second positional argument
      """
        # Can be user-defined rules
        charge_direction = [0] * 24
        for i in range(24):
            if self.price_label[i] == 2:  # Discharge when price is high
                charge_direction[i] = -1
            elif self.price_label[i] == 1:  # Charge when price is low
                charge_direction[i] = 0
            elif self.price_label[i] == 0:  # No action when price is normal
                charge_direction[i] = 1

        return charge_direction
